package com.example.controller;

import com.example.common.Result;
import com.example.entity.Goods;
import com.example.service.GoodsService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: 康伟奇
 * @date: 2023 - 11 - 13 11:34
 **/

/**
 * 商品信息前端操作接口
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    //新增
    @PostMapping("/add")
    public Result add(@RequestBody Goods goods){
        goodsService.add(goods);
        return Result.success();
    }

    //删除
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id){
        goodsService.deleteById(id);
        return Result.success();
    }

    //批量删除
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids){
        goodsService.deleteBatch(ids);
        return Result.success();
    }

    //修改
    @PutMapping("/update")
    public Result updateById(@RequestBody Goods goods){
        goodsService.updateById(goods);
        return Result.success();
    }

    //根据id查询
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id){
        Goods goods = goodsService.selectById(id);
        return Result.success(goods);
    }

    //查询所有
    @GetMapping("/selectAll")
    public Result selectAll(Goods goods){
        List<Goods> list = goodsService.selectAll(goods);
        return Result.success(list);
    }

    //分页查询
    @GetMapping("/selectPage")
    public Result selectPage(
            Goods goods,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize
    ){
            PageInfo<Goods> page = goodsService.selectPage(goods,pageNum,pageSize);
            return Result.success(page);
    }

}
